﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 图书借阅系统
{
    public partial class 管理员书名查询 : System.Web.UI.Page
    {
        private void BindGridView1()
        {
            string tta = "select * from 图书 where 书名 like '%" + TextBox1.Text + "%'";
            GridView1.DataSource = Connection.getData(tta);
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindGridView1();
        }

        /*protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            int lkbt = GridView1.SelectedIndex;
            string tt = GridView1.Rows[lkbt].Cells[1].Text.ToString();
            string str = "select 实际数量 from 图书 where id = " + tt;

            int i= Convert.ToInt32( TextBox2.Text);
            if (Connection.getNum(str) < i)
            {
                Label2.Text = "并没有那么多书！";
            }
            else
            {
                string sa = "update 图书 set 实际数量 = 实际数量- " + i + "where id = " + tt;
                string saa = "update 图书 set 总数 = 总数- " + i + "where id = " + tt;
                if (Connection.setData(sa) != -1 && Connection.setData(saa) != -1)
                {
                    Label2.Text = "删除成功！";
                    string sttr = "select 总数 from 图书 where id = " + tt;
                    if (Connection.getNum(sttr) == 0)
                    {
                        string a = "delete from 图书 where id =" + tt;
                        Connection.setData(a);
                    }
                }
                else { Label2.Text = "删除失败！"; }
            }
            BindGridView1();*/

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "deleteBook")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int id = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text.ToString());
                int sum = Convert.ToInt32(GridView1.Rows[index].Cells[3].Text.ToString());
                int num = Convert.ToInt32(GridView1.Rows[index].Cells[4].Text.ToString());

                if (sum == num)
                {
                    string str = @"delete  from 图书 where id=" + id + "";

                    Connection.setData(str);
                }
                else
                {
                    string str1 = @"update 图书
                                   set 总数=总数-实际数量,实际数量=0
                                   where id=" + id + "";

                    Connection.setData(str1);
                }
                BindGridView1();
            }
            

             if (e.CommandName == "into")
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    int id = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text.ToString());
                    
                    String name = GridView1.Rows[index].Cells[1].Text.ToString();
                    Session["BookName"] = name;                   
                    int sum = Convert.ToInt32(GridView1.Rows[index].Cells[3].Text.ToString());
                    Session["BookSum"] = sum;
                    int num = Convert.ToInt32(GridView1.Rows[index].Cells[4].Text.ToString());
                    Session["BookNum"] = num;
                    
                    Response.Redirect("修改图书.aspx");
                    

                }
            }

        
                    
    }
}
    
